package com.zb.nio.files;

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;

import org.junit.Test;

/**
 * @author: zhaobin
 * @date: 2024/2/24 17:20
 * @Description:
 */
public class FilesDemo {

    @Test
    public void testCreateDirectory() {
        // createDirectory
        Path path = Paths.get("./src/main/resources/abc");
        // 创建能目录
        try {
            Files.createDirectory(path);
        }catch (FileAlreadyExistsException e) {
            System.out.println("目录已存在"+ e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testCopy() {
        Path path1 = Paths.get("./src/main/resources/01.txt");
        Path path2 = Paths.get("./src/main/resources/02.txt");
        try {
            Path copy = Files.copy(path1, path2);
            // 如果目标文件已经存在，StandardCopyOption.REPLACE_EXISTING这个参数指示 copy() 方法覆 盖现有的文件
            // Path copy = Files.copy(path1, path2, StandardCopyOption.REPLACE_EXISTING);
        } catch (FileAlreadyExistsException e) {
            // 文件已存在
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testMove() {
        Path path1 = Paths.get("./src/main/resources/01.txt");
        Path path2 = Paths.get("./src/main/resources/11.txt");
        try {
            Path move = Files.move(path1, path2);
        } catch (FileAlreadyExistsException e) {
            // 文件已存在
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testDelete() {
        Path path = Paths.get("./src/main/resources/01.txt");
        try {
            Files.delete(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 遍历查找
     */
    @Test
    public void walkFileTree(){
        // 要查找的目录范围
        Path path = Paths.get("./src/main/resources");
        // 要查找的目标文件
        //String findFile = File.separator + "03.txt";
        String findFile = "01.txt";

        // 是否找到
        final boolean[] isExist = {false};

        try {
            Path path1 = Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                /**
                 * 重载内部类方法
                 * @param file
                 * @param attrs
                 * @return
                 * @throws IOException
                 */
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileString = file.toAbsolutePath().toString();

                    // 如果目标文件和路径结尾相等，即为找到
                    if (fileString.endsWith(findFile)) {
                        // 把查找标记为true
                        isExist[0] = true;
                        System.out.println("文件已找到！路径为：" + fileString);
                        // 中止退出
                        return FileVisitResult.TERMINATE;
                    }
                    // 继续下次查找
                    return FileVisitResult.CONTINUE;
                }
            });
            if(!isExist[0]){
                System.out.println("没有找到！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
